package com.cuckoo.cuckoo_app2.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cuckoo.cuckoo_app2.R;
import com.cuckoo.cuckoo_app2.dao.ItemWallet;
import com.cuckoo.cuckoo_app2.dao.PlatList;
import com.cuckoo.cuckoo_app2.enums.PaymentType;

import java.util.List;

public class WalletTradeAdpter extends RecyclerView.Adapter<WalletTradeAdpter.ViewHolder> {

    private Context context;
    private List<ItemWallet> dataList;


    public WalletTradeAdpter(Context context, List<ItemWallet> dataList) {
        this.context = context;
        this.dataList = dataList;

    }


    @NonNull
    @Override
    public WalletTradeAdpter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_wallet_compon, parent, false);
        return new WalletTradeAdpter.ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ItemWallet item = dataList.get(position);
        PaymentType paymentType = PaymentType.fromCode(item.getTransactionType());
        // 待处理
        if (item.getStatus() == 3) {
            paymentType = PaymentType.WITHDRAWAL;
        }

        holder.item_wallet_type.setText(paymentType.getType());
        holder.item_wallet_type.setTextColor(paymentType.getColor());

        holder.item_wallet_symbol.setText(paymentType.getSymbol());

        String money = "¥" + item.getAmount();
        holder.item_wallet_money.setText(money);

        holder.item_wallet_time.setText(item.getCreateTime());
        String content = "¥" + item.getBalance();
        holder.item_wallet_balance.setText(content);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    // 清空数据并通知更新
    public void clearData() {
        this.dataList.clear(); // 假设 mData 不是 null
        notifyDataSetChanged();
    }

    // 更新数据列表的方法
    public void updateData(List<ItemWallet> dataList) {
        this.dataList = dataList; // 直接替换数据列表
        notifyDataSetChanged(); // 通知RecyclerView数据已更改
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView item_wallet_type;
        TextView item_wallet_symbol;
        TextView item_wallet_money;
        TextView item_wallet_time;
        TextView item_wallet_balance;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            item_wallet_type = itemView.findViewById(R.id.item_wallet_type);
            item_wallet_symbol = itemView.findViewById(R.id.item_wallet_symbol);
            item_wallet_money = itemView.findViewById(R.id.item_wallet_money);
            item_wallet_time = itemView.findViewById(R.id.item_wallet_time);
            item_wallet_balance = itemView.findViewById(R.id.item_wallet_balance);
        }
    }
}
